
#include "Gene.h"


Gene::Gene(Box * pBox)
 : m_pBox( pBox )
{
	
}

Gene::~Gene(void)
{
}

void Gene::setHead(string head)
{
	this->m_head=head;
}

void Gene::setTail(string tail)
{
	this->m_tail=tail;
}

void Gene::setWhole(vector<string> whole)
{
	string head="";//=whole.substr(0, this->m_pBox->headLength);
	string tail="";//=whole.substr(this->m_pBox->headLength, this->m_pBox->tailLength);
	for(int i=0;i<this->m_pBox->headLength;i++){
		head+=whole[i];
	}
	for(int i=this->m_pBox->headLength;i<this->m_pBox->geneLength;i++) {
		tail+=whole[i];
	}
	this->m_head=head;
	this->m_tail=tail;
}

string Gene::getHead()
{
	return this->m_head;
}

string Gene::getTail()
{
	return this->m_tail;
}

string Gene::getWhole()
{
	string tmp=this->m_head+this->m_tail;
	return tmp;
}

double Gene::evaluate(int FCNo)
{
	//this->m_pSRTRoot->print();
	
	//std::cout<<"?? G::ev ("<<this->m_pSRTRoot->value(FCNo)<<")"<<std::endl;
	return this->m_pETRoot->valueD(FCNo);			// TEMP
}
bool Gene::boolEvaluate(int FCNo)
{
	return this->m_pETRoot->valueB(FCNo);
}

ExpressionNode* Gene::getExpressionTreeRoot()
{
	return this->m_pETRoot;
}

vector<int> Gene::getRealPos(int pos)
{
	//int realsize=this->head.size()+this->tail.size();
	string tmp=this->getWhole();
	vector<int> ret;
	int ln;
//int pos;
	int j=0;
	int p=0;

	while(j<=pos && j<tmp.size()){
		if(this->m_pBox->isFunction(tmp[j])) {
			ln=1;
			j++;
			p++;
		}
		else{
			string sub=tmp.substr(j,tmp.size());
			string breakers=this->m_pBox->getFunctions()+this->m_pBox->getTermChars();
			//j=sub.find_first_of(breakers);
			j++;
		//	int posEnd=
		}
	}
	return ret;
}
vector<string> Gene::stringToVec(){
	string Str=this->getWhole();
	return stringToVec(Str);
}

vector<string> Gene::stringToVec(string tempGen)
{
	string breakers=this->m_pBox->getFunctions()+this->m_pBox->getTermChars();
	//string breakers="g";
	vector<string> vecGen;
	//tempGen="g"+tempGen;
	int found=tempGen.find_first_of(breakers);
	while (found!=-1)
	{
	//	 if(found >= 0){
			std::istringstream stm;
			int endpos=tempGen.find_first_of(breakers, found+1);
			stm.str(tempGen.substr(found,endpos));

			//string value=inLine.substr(0,found);
			string d;//=std::atof(value);
			stm >>d;
			vecGen.push_back(d);
	//	 }
		if(endpos!= -1) {
			tempGen = tempGen.substr(endpos);
		}
		else{
			tempGen="";
		}
		found = tempGen.find_first_of(breakers);
		
	}
	if(tempGen.length() > 0){
		std::istringstream last;
		last.str(tempGen);
		string f;
		last >> f;
		//std::cout<<"?? SRlTV: f (last): "<<f<<std::endl;
		vecGen.push_back(f);
	}
	return vecGen;
} 